package com;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;

public class AutoGenerator {
	// 处理 all 情况
	public static void main(String[] args) {
		List<String> tables = new ArrayList<>();
		System.out.println(System.getProperty("user.dir"));
		
		FastAutoGenerator.create("jdbc:mysql://127.0.0.1:3306/easybbs?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true","root","root")
				.globalConfig(builder -> {
					builder.author("程其红")               //作者
							.outputDir(System.getProperty("user.dir")+"\\common"+"\\src\\main\\java")    //输出路径(写到java目录)
							.enableSwagger()           //开启swagger“
							.commentDate("yyyy-MM-dd")
							.fileOverride();            //开启覆盖之前生成的文件
					
				})
				.packageConfig(builder -> {
					builder.parent("com")
							.moduleName("practice")
							.entity("entity")
							.service("service")
							.serviceImpl("service.impl")
							.controller("controller")
							.mapper("mapper")
							.xml("mapper")
							.pathInfo(Collections.singletonMap(OutputFile.mapperXml,System.getProperty("user.dir")+"\\common"+"\\src\\main\\resources\\mapper"));
				})
				.strategyConfig(builder -> {
					builder.addInclude(tables)
							.addTablePrefix("p_")
							.serviceBuilder()
							.formatServiceFileName("%sService")
							.formatServiceImplFileName("%sServiceImpl")
							.entityBuilder()
							.enableLombok()
							.logicDeleteColumnName("deleted")
							.enableTableFieldAnnotation()
							.controllerBuilder()
							// 映射路径使用连字符格式，而不是驼峰
							.enableHyphenStyle()
							.formatFileName("%sController")
							.enableRestStyle()
							.mapperBuilder()
							//生成通用的resultMap
							.enableBaseResultMap()
							.superClass(BaseMapper.class)
							.formatMapperFileName("%sMapper")
							.enableMapperAnnotation()
							.formatXmlFileName("%sMapper");
				})
				.templateConfig(new Consumer<TemplateConfig.Builder>() {
					@Override
					public void accept(TemplateConfig.Builder builder) {
						// 实体类使用我们自定义模板
						builder.entity("templates/myentity.java");
					}
				})
				.templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
				.execute();
	}

}
